﻿using Core.Domain;
using Core.DomainServices;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.DomainServices
{
    public class EmployeeRepository : IDataRepository
    {
        public IList<Employee> GetAll()
        {
            IList<Employee> items = new List<Employee>()
            {
                new Employee{EmployeeId = "001", Comment=string.Empty, Day = DateTime.Now.AddDays(-6), STHours = 8},
                new Employee{EmployeeId = "001", Comment="Overtime for to do something ",Day = DateTime.Now.AddDays(-6), OTHours = 1},

                new Employee{EmployeeId = "001", Comment="AM", Day = DateTime.Now.AddDays(-5), STHours = 6},
                new Employee{EmployeeId = "001", Comment="Overtime for to do something",Day = DateTime.Now.AddDays(-5), OTHours = 2},

                new Employee{EmployeeId = "002", Comment="AM", Day = DateTime.Now.AddDays(-6), STHours = 8},
                new Employee{EmployeeId = "002", Comment="PM",Day = DateTime.Now.AddDays(-6), OTHours = 0},

                new Employee{EmployeeId = "002", Comment="AM", Day = DateTime.Now.AddDays(-5), STHours = 4},
                new Employee{EmployeeId = "002", Comment="Overtime for to do something",Day = DateTime.Now.AddDays(-5), OTHours = 4},
            };

            return items;
        }

        public IList<Employee> GetTimesheets(IList<Employee> items)
        {
            if (items == null || !items.Any()) return null;

            IList<Employee> results = new List<Employee>();
            foreach(var item in items){
                item.KeyId = item.EmployeeId + ";#" + item.Day.Value.ToString(Constants.FORMAT_DATE_TIME);
                results.Add(item);
            }

            var resultLines = results.GroupBy(p => p.KeyId)
                .Select(d => new Employee { 
                    KeyId = d.First().KeyId,
                    Day = d.First().Day,
                    Comment = string.Join("\n", d.Select(c=>c.Comment)),
                    EmployeeId = d.First().EmployeeId,
                    OTHours = d.Sum(c=>c.OTHours),
                    STHours = d.Sum(c=>c.STHours)

                }).ToList();
            return resultLines;
        }
    }
}
